using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.Web.BL.Rss;
using NTTHAO.CommonWeb;
using System.Collections.Generic;

public partial class admin_webparts_Plug_ins_RssNews_RssMenuAddLink : System.Web.UI.UserControl
{
    public RssPageGroup _Group = null;
    public RssMenu _Menu = null;
    public DataTable _List = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnSave.Text = this.btnSave2.Text = LanguageUtility.GetLanguageText("SAVE", Session);

        int siteID = WebGlobal.GetSite().ID;
        int langID = LanguageUtility.GetLanguageID();
        bool isAdmin = Context.User.Identity.Name.Equals("admin", StringComparison.OrdinalIgnoreCase);
        try
        {
            _Group = new RssPageGroup(Convert.ToInt32(Request.QueryString["g"].Trim()));
            _Menu = new RssMenu(Convert.ToInt32(Request.QueryString["m"].Trim()));
            if (_Group.ID < 1 || _Menu.ID < 1)
            {
                throw new Exception("This function is invalid.");
            }
            _List = RssMenuLink.GetAllLinks(_Menu.ID, langID, isAdmin ? -1 : 1);
        }
        catch
        {
            Response.Redirect("?type=invalidurl&ReturnUrl=" + Server.UrlEncode(Request.RawUrl), false);
            return;
        }
    }
    public void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string linksStr = Request.Params["links"];
            string[] links = new string[0];
            if (linksStr != null)
            {
                links = linksStr.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            }
            
            List<int> linkList = new List<int>();
            if (links != null)
            {
                foreach (string link in links)
                {
                    linkList.Add(Convert.ToInt32(link.Trim()));
                }
            }

            RssMenuLink.UpdateLinks(_Menu.ID, linkList.ToArray());

            string bUrl = "?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&item=" + Request.QueryString["item"] + "&g=" + Request.QueryString["g"] + "&m=" + Request.QueryString["m"] + "&action=detail";
            Response.Redirect(bUrl, false);
            return;
        }
        catch (Exception ex)
        {
            HtmlGenericControl ctrl = new HtmlGenericControl("div");
            ctrl.InnerHtml = "<p>" + ex.Message + "</p><p>" + ex.StackTrace + "</p>";
            this.Controls.Add(ctrl);
        }
    }
}
